package com.ccjq.jq173.shoppingcart;

import com.ccjq.jq173.core.util.UuidUtils;
import com.ccjq.jq173.member.po.User;
import com.ccjq.jq173.product.po.Category;
import com.ccjq.jq173.product.po.Product;
import com.ccjq.jq173.product.service.ProductService;
import com.ccjq.jq173.product.service.impl.ProductServiceimpl;
import com.ccjq.jq173.shopping.po.ShoppingCart;
import com.ccjq.jq173.shopping.service.ShoppingCartService;
import com.ccjq.jq173.shopping.service.impl.ShoppingCartServiceImpl;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class shoppingServlet extends HttpServlet {
    private static final String FLAG_SC="sc";
    private static final String FLAG_XG="xg";
    private static final String FLAG_GX="gx";
    private static final String FLAG_TJ="tj";
    private static final String FLAG_JS="js";
    private static final String FLAG_AN="an";
    private static List<ShoppingCart> list=null;
    private static String str;
    private ShoppingCartService shoppingCartService;
    private ProductService productService;
    private ProductService service =null;
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding( "utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        String flag =request.getParameter( "flag" );
        if(StringUtils.isBlank(flag)) {
            bl(request,response);
        }
        else if (FLAG_SC.equals( flag )) {
            sc( request, response );
        } else if (FLAG_XG.equals( flag )) {
            xg( request, response );
        } else if (FLAG_GX.equals( flag )) {
            //gx( request, response );
        } else if (FLAG_TJ.equals( flag )) {
            tj( request, response );
        } else if (FLAG_JS.equals( flag )) {
            js( request, response );
        } else if (FLAG_AN.equals( flag )) {
            an( request, response );
        }else{
            request.setAttribute( "t","请登录" );
        }
    }
    public void bl(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String userId = request.getParameter( "userId" );
        if(request.getSession().getAttribute( "user" ) == null){
            request.getRequestDispatcher( "/WEB-INF/jsp/member/login.jsp" ).forward( request,response );
        }else {
            shoppingCartService = new ShoppingCartServiceImpl();
            service = new ProductServiceimpl();

            list = shoppingCartService.queryShoppings( userId );
            double zong = shoppingCartService.aggregate( list );
            request.setAttribute( "zong", zong );
            request.setAttribute( "shoppingcart", list );

            List<Product> hotSales = service.queryHotSales();
            List<Category> categorys = service.queryCategorys();
            request.setAttribute( "categorys", categorys );
            request.setAttribute( "hotSales", hotSales );
            request.getRequestDispatcher( "/WEB-INF/jsp/shoppingcart/shoppingCar.jsp" ).forward( request, response );
        }
    }

    public void sc(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        shoppingCartService = new ShoppingCartServiceImpl();
        String productId =request.getParameter( "productId" );
        String cartId =request.getParameter( "cartId" );
        String userId = request.getParameter( "userId" );
        System.out.println( userId );
        shoppingCartService.deleteShoppingCart(cartId,productId,userId);
        bl(request,response);
    }
    public void tj(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        shoppingCartService = new ShoppingCartServiceImpl();
        String quantity = request.getParameter( "quantity" );
        String productId = request.getParameter( "productId" );
        String userId = request.getParameter( "user_id" );
        String userid= UuidUtils.generateShortUUID("U",10);
        int resl = productService.getStockByProductId(productId);
        int reslut=0;
        if(resl > Integer.parseInt( quantity )){
            reslut = shoppingCartService.saveShoppingCart(Integer.parseInt( quantity ),userid,userId,productId);
            if(reslut !=0){
                response.getWriter().write( "成功" );
            }else{
                response.getWriter().write( "修改失败" );
            }
        }else{
            response.getWriter().write( "修改失败" );
        }
        List<ShoppingCart> list= shoppingCartService.queryShoppings( userId );
        request.getRequestDispatcher( "/WEB-INF/jsp/shoppingcart/shoppingCar.jsp" ).forward( request,response );

    }

    public void xg(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        shoppingCartService = new ShoppingCartServiceImpl();
        productService =new ProductServiceimpl();
        String quantity  =  request.getParameter( "quantity" );
        String productId = request.getParameter( "productId" );
        String userId = request.getParameter( "userId" );
        int resl = productService.getStockByProductId(productId);
        int reslut=0;
        if(resl > Integer.parseInt( quantity )){
            reslut = shoppingCartService.updatShoppingCart(Integer.parseInt( quantity ),productId,userId);
            if(reslut !=0){
                response.getWriter().write( "修改成功" );
            }else{
                response.getWriter().write( "修改失败" );
            }
        }else{
            response.getWriter().write( "修改失败" );
        }
        /*list = shoppingCartService.queryShoppings( userId );
        System.out.println( userId );
        double zong = shoppingCartService.aggregate( list );
        request.setAttribute( "zong",zong );
        System.out.println( zong );
        request.setAttribute( "shoppingcart",list );
        request.getRequestDispatcher( "/WEB-INF/jsp/shoppingcart/shoppingCar.jsp" ).forward( request,response );*/

    }
    public void js(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        shoppingCartService = new ShoppingCartServiceImpl();

        String order_id= UuidUtils.generateShortUUID("U",10);
        User u=(User)request.getSession().getAttribute( "user" );
        shoppingCartService.settleAccounts(
                order_id,//订单ID
                Integer.parseInt( request.getParameter( "payment" )),//支付类型
                Double.parseDouble( str),//总金额
                request.getParameter( "receiver" ),//收货人姓名
                request.getParameter( "province" ),//省份
                request.getParameter( "city" ),//城市
                request.getParameter( "county" ),//区县
                request.getParameter( "address" ),//详细地址
                request.getParameter( "zipcode" ) ,//邮编
                request.getParameter( "telphone" ),//联系电话
                u.getUserId());//用户ID*/
        shoppingCartService.updateT_ORDER_PRODUCT( order_id,list );
        for(Object o:list){
            ShoppingCart shoppingCart =(ShoppingCart)o;
            shoppingCartService.updateStack(shoppingCart.getProductId(),shoppingCart.getQuantity());
            shoppingCartService.deleteShoppingCart(shoppingCart.getShoppingCarId(),shoppingCart.getProductId(),
                    shoppingCart.getUserId());
        }
        list.clear();
        response.sendRedirect( "http://localhost:8080/order" );

    }
    public void an(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String zong =request.getParameter( "zong" );
        request.setAttribute( "zong",zong );
        str=zong;
        List<Product> hotSales = service.queryHotSales();
        List<Category> categorys = service.queryCategorys();
        request.setAttribute( "categorys", categorys );
        request.setAttribute( "hotSales", hotSales );
        request.getRequestDispatcher( "/WEB-INF/jsp/shoppingcart/balance.jsp").forward( request,response );

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost( request,response );
    }
}
